<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="物业名称" prop="pmid">
        <el-select v-model="queryParams.pmid" placeholder="请选择物业名称" @change="clearCarshomeList"
                   clearable>
          <el-option
            v-for="item in this.propertyList"
            :key="item.pmid"
            :label="item.propertyName"
            :value="item.pmid">
          </el-option>
        </el-select>
      </el-form-item>




      <el-form-item label="车场名称" prop="chid">
        <el-select v-model="queryParams.chid" placeholder="请选择车场名称" @visible-change="getListCars" clearable>
          <el-option
            v-for="item in this.carshomeList"
            :key="item.chid"
            :label="item.chname"
            :value="item.chid">
          </el-option>
        </el-select>
      </el-form-item>




      <el-form-item label="岗亭名称" prop="boothname">
        <el-input
          v-model="queryParams.boothname"
          placeholder="请输入岗亭名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sys-cars-home:booth:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sys-cars-home:booth:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sys-cars-home:booth:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sys-cars-home:booth:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="boothList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="岗亭ID" align="center" prop="chbid" />
      <el-table-column label="物业名称" align="center" prop="propertyManagement.propertyName" />
      <el-table-column label="车场名称" align="center" prop="carsHome.chname" />
      <el-table-column label="岗亭名称" align="center" prop="boothname" />
      <el-table-column label="岗亭IP" align="center" prop="boothip" />
      <el-table-column label="所处区域" align="center" prop="boothaddr" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
          >查看</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sys-cars-home:booth:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sys-cars-home:booth:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listBooth, getBooth, delBooth, addBooth, updateBooth } from "@/api/modules/sys-cars-home/booth/booth";
import {listHome} from "@/api/modules/sys-cars-home/home";
import path from "path";
import {listProperty} from "@/api/modules/sys-property/property";

export default {
  name: "Booth",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车场岗亭表格数据
      boothList: [],
      // 车场管理表格数据
      carshomeList: [],
      // carshomeList2: [],

      // 物业数据
      propertyList: [],
      // 查询参数
      queryParams: {

        pageNum: 1,
        pageSize: 10,

        boothname: null,
        boothip: null,
        boothaddr: null,
        code: null,
        status: null,
        deleted: null,

        chid: null,
        pmid:null,
      },
      // 查询参数
      queryParams2: {
        pmid: null,
      },
      // 表单参数
      form: {
        chbid: null,
        chid: null,
        boothname: null,
        boothip: null,
        boothaddr: null,
        remark: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        carsHome:{
          chid:null,
          chname:null,
          addr:null,
        },
        propertyManagement:{
          pmid:null,
          propertyName:null,
        },
      },
      // 表单校验
      rules: {
        boothname: [
          { required: true, message: "岗亭名称不能为空", trigger: "blur" }
        ],
        boothip: [
          { required: true, message: "岗亭ip不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "唯一编码,前台使用不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        deleted: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getListCars()
    this.getListProperty()
  },
  methods: {


    /** 查询车场岗亭列表 */
    getList() {
      this.loading = true;
      listBooth(this.queryParams).then(response => {
        this.boothList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },



    /** 查询车场管理列表 */
    getListCars() {
      this.queryParams2.pmid = this.queryParams.pmid
      listHome(this.queryParams2).then(response => {
        this.carshomeList = response.rows;
      });
      // listHome(null).then(response => {
      //   this.carshomeList2 = response.rows;
      // });
    },




    /** 查询物业列表 */
    getListProperty() {
      this.loading = true;
      listProperty(this.queryParams).then(response => {
        this.propertyList = response.rows;
        this.loading = false;
      });
    },
    //清除车场
    clearCarshomeList() {
      this.queryParams2.pmid = this.queryParams.pmid
      this.queryParams.chid = null
      this.getListCars()
    },











    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chbid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: 'boothedit',
        query: {
          operate: "新增",
        }
      });
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chbid = row.chbid || this.ids
      this.$router.push({
        path: 'boothedit',
        query: {
          chbid: chbid,
          operate: "修改",
        }
      });
    },

    /** 查看按钮操作 */
    handleLook(row) {
      const chbid = row.chbid || this.ids
      this.$router.push({
        path: 'boothedit',
        query: {
          chbid: chbid,
          disabled: true,
          operate: "查看",
        }
      });
    },




    /** 删除按钮操作 */
    handleDelete(row) {
      const chbids = row.chbid || this.ids;
      this.$modal.confirm('是否确认删除车场岗亭编号为"' + chbids + '"的数据项？').then(function() {
        return delBooth(chbids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sys-cars-home/booth/export', {
        ...this.queryParams
      }, `booth_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
